﻿using System;
using System.Collections;
using System.Net;
using System.Text;

using GameServer.Networking;
using GameServer.Networking.Handlers;

namespace GameServer.Managers
{
    class PacketManager
    {
        private static Hashtable _Packets = new Hashtable();

        ~PacketManager()
        {
       
        }

        public static void setup()
        {
            _Packets = new Hashtable();
            //addPacket(91337, new HANDLE_WELCOME_PACKET()); // WRPSP Login
            
            addPacket(24576, new HANDLE_LOGOUT());
            addPacket(24832, new HANDLE_WELCOME_PACKET()); //24832
            addPacket(25088, new HANDLE_CHARACTER_INFO());
            addPacket(29472, new HANDLE_QUICK_JOIN());
            addPacket(25600, new HANDLE_PING());
            addPacket(28673, new HANDLE_CHANNEL_SWITCH());
            addPacket(29184, new HANDLE_ROOM_LIST());
            addPacket(25588, new HANDLE_TRINITYGUARD_CHECK());
            addPacket(29488, new HANDLE_SPECTATE_ROOM());
            addPacket(29440, new HANDLE_ROOM_CREATION());
            addPacket(29456, new HANDLE_ROOM_JOIN());
            addPacket(29504, new HANDLE_ROOM_LEAVE());
            addPacket(29505, new HANDLE_ROOM_KICK());
            addPacket(29520, new HANDLE_ROOM_INVITE());
            addPacket(29696, new HANDLE_CHAT());
            addPacket(29970, new HANDLE_EQUIPMENT());
            addPacket(29984, new HANDLE_ROOM_BOMB());
            addPacket(30000, new HANDLE_ROOM_DATA());
            addPacket(30032, new HANDLE_SCORE_BOARD());
            addPacket(30208, new HANDLE_ITEMSHOP());
            addPacket(28960, new HANDLE_USER_LIST());
            addPacket(25605, new HANDLE_COUPON_EVENT());
            addPacket(25606, new HANDLE_COUPON_BUY());
            addPacket(30257, new HANDLE_LUCKY_SHOT());
            addPacket(30258, new HANDLE_LUCKY_SHOT_WIN());
            addPacket(30259, new HANDLE_LUCKY_SHOT());
            addPacket(30224, new HANDLE_DELETE_WEAPON());
            addPacket(30720, new HANDLE_CREDITS());
            addPacket(30225, new HANDLE_DELETE_CostumeItem());
            addPacket(29971, new HANDLE_COSTUME_EQUIPMENT());
            addPacket(30209, new HANDLE_CostumeItem_BUY_DINAR());
            addPacket(30230, new HANDLE_COSTUME_BUY());
            addPacket(31490, new HANDLE_MULTIPLAYER_ZOMBIE());
            addPacket(29969, new HANDLE_LEAVE_VEHICLE());
            addPacket(31492, new HANDLE_SKILL_POINT());
            //addPacket(30209, new HANDLE_CostumeItem_BUY_2());
            //addPacket(24593, new HANDLE_FIND_USER());
        }

        public static PacketHandler parsePacket(byte[] pBytes)
        {
            string packetStr = System.Text.Encoding.Default.GetString(pBytes);

            try
            {
                string[] tBlocks = packetStr.Split(Convert.ToChar(0x20));
                long Timestamp = long.Parse(tBlocks[0]);
                int PacketID = int.Parse(tBlocks[1]);

                if (_Packets.ContainsKey(PacketID))
                {
                    //if (Config.Debug == 1)
                        //Log.WriteDebug("Received Packet -> " + packetStr);
                    string[] _Blocks = new string[tBlocks.Length - 2];
                    Array.Copy(tBlocks, 2, _Blocks, 0, tBlocks.Length - 2);
                    PacketHandler pHandler = (PacketHandler)_Packets[PacketID];
                    pHandler.set(Timestamp, PacketID, _Blocks);
                    return pHandler;
                }
                else
                {
                    if (Config.Debug == 1)
                    {
                        Log.WriteError("Unknown Packet ID: " + PacketID);
                        Log.WriteError("Packet -> " + packetStr);
                    }
                }
            }
            catch { };

            return null;
        }

        private static void addPacket(int ID, PacketHandler Handler)
        {
            if (_Packets.ContainsKey(ID) == false)
            {
                _Packets.Add(ID, Handler);
            }
            else
            {
                Log.WriteError("Packet Manager already contains packetID: " + ID.ToString());
            }
        }

        public static string IPToAddr(long address)
        {
            return IPAddress.Parse(address.ToString()).ToString();
        }

        public static long IPToInt(string addr)
        {
            return (long)((ulong)IPAddress.NetworkToHostOrder((int)IPAddress.Parse(ReverseIP(addr)).Address));
        }

        private static string ReverseIP(string tString)
        {
            try
            {
                string[] strArray = tString.Split(new char[] { '.' });
                string str = "";
                for (int i = strArray.Length - 1; i > -1; i--)
                {
                    str = str + strArray[i] + ".";
                }
                return str.Substring(0, str.Length - 1);
            }
            catch (Exception ex) { Log.WriteError(ex.Message); return "-1"; }
        }

        public static int ReversePort(int iPort)
        {
            try
            {
                char[] array = iPort.ToString().ToCharArray();
                Array.Reverse(array);
                return int.Parse(new string(array));
            }
            catch (Exception ex) { Log.WriteError(ex.Message); return -1; }
        }
    }
}
